<?php
namespace app\home\controller;





use app\common\model\Config;
use app\Request;

class Withdrawal
{
    public function applyWithdrawal(Request $request){
       $param  = $request->param();
       $withdrawal_commission_condition  = Config::getConfigValue('withdrawal_condition');
       if ($request->userInfo['surplus_income'] <$withdrawal_commission_condition ){
           return error('收益不足提现设置条件');
       }
        $withdrawalModel = new \app\common\model\Withdrawal();
        $withdrawalList = $withdrawalModel->where('create_time','>',strtotime(date('Y-m-d 00:00:00')))
            ->where('uid','=',$request->userInfo['id'])->select()->toArray();
        $amount= array_column($withdrawalList, 'amount');
        $totalAmount = array_sum($amount);
        $everyday_withdrawal_amount =  Config::getConfigValue('everyday_withdrawal_amount');
        if ( $totalAmount >$everyday_withdrawal_amount ){
            return error('每日提现金额不能超过'.$everyday_withdrawal_amount);
        }
        $everyday_withdrawal_num=  Config::getConfigValue('everyday_withdrawal_num');
        if (count($withdrawalList) >= $everyday_withdrawal_num ){
            return error('每日提现次数不能超过'.$everyday_withdrawal_num);
        }
        $withdrawal_proportion  = Config::getConfigValue('recharge_ratio');
        $withdrawal_fees_ratio = Config::getConfigValue('withdrawal_fees_ratio');
        $fees = bcdiv(bcmul($param['number'],$withdrawal_fees_ratio,2),100,2); // 平台币
        $money =  bcdiv( bcsub($param['number'],$fees,2),$withdrawal_proportion,2);
        $data = [
            'uid' => $request->userInfo['id'],
            'sn_order' =>  getOrderNo('w'),
            'ratio'  =>$withdrawal_proportion ,// 平台币和金额比例
            'amount' => $money,
            'pay_mode' => $param['pay_mode'],
            'create_time' => time(),
            'status' => 1,
            'fees' =>  $fees,
            'number' => $param['number'],
            'fees_ratio' => $withdrawal_fees_ratio,
        ];
        if(isset($param['name'])){
            $data['name'] =  $param['name'];
        }
        if(isset($param['name'])){
            $data['account'] = $param['account'];
        }
        $withdrawalModel->insert($data);
        $userModel = new \app\common\model\User();
        $userModel ->where(['id' => $request->userInfo['id']])
            ->dec('surplus_income', $param['number'])
            ->update();
        return success('提交成功,等待管理员审核');
    }
    public  function  withdrawalList(Request $request){
        $param= $request->param();
        $withdrawalModel = new \app\common\model\Withdrawal();
        $where['uid'] = $request->userInfo['id'];
        if (isset($param['pay_mode']) && !empty($param['pay_mode'])){
            $where['pay_mode'] = $param['pay_mode'];
        }
        $limit = 10;
        $start = 0;
        if (isset($param['limit']) && !empty($param['limit'])) {
            $limit = $param['limit'];
        }
        if (isset($param['page']) && !empty($param['page'])) {
            $start = ($param['page'] - 1) * $limit;
        }
        $list =  $withdrawalModel->where($where)->limit($start,$limit)->select()->toArray();
        $count = $withdrawalModel->where($where)->count();
        return success('提现列表',['list' => $list,'count' => $count]);
    }
    public function  withdrawalInfo(Request $request){
        $param = $request->param();
        $withdrawalModel = new \app\common\model\Withdrawal();
        $where['id'] =$param['id'];
        $list =  $withdrawalModel->where($where)->find();

        return success('订单详情',$list);
    }
}
